﻿package ua.mihaylenko.cardsplugin.common{
	import flash.net.URLRequest;
	import flash.net.URLLoader;
	import flash.events.Event;
	import flash.events.SecurityErrorEvent;	
	import flash.events.IOErrorEvent;
	import flash.xml.*;
	import ua.mihaylenko.cardsplugin.data.*;
	public dynamic class XMLParcer{
		private var cURLLoader:URLLoader;
		private var _fCallBackObject:Object;
		private var sURL:String;
		private var _aThumbs:Array;
		public function XMLParcer(fCallBackObject:Object){
			_fCallBackObject = fCallBackObject;
			if(FlashVars.getInstance().getData("params")){
				sURL = new String(FlashVars.getInstance().getData("params"));
			}else{
				sURL=new String("http://order.uprintinvitations.com/RssTemplates.aspx?3046780C-0F5B-4DD5-AAF7-5C1499FE3D6C");
				//sURL=new String("http://zetaprints.com/RssTemplates.aspx?0AFC70BA-F856-42D9-B707-FFF27753F0F0");
				//sURL = new String("http://zetaprints.com/RssTemplates.aspx?6F704709-CB81-4B62-9259-DF07804FF60B");
				//6F704709-CB81-4B62-9259-DF07804FF60B");
				//2D245F1D-BAFE-47B5-93E5-63805754A54B
				//A1DC2EFF-4707-4335-B454-A564417D84CC
			}
			var aDomain:Array = sURL.split("/");
			aDomain.splice(3, aDomain.length-1);
			
			FlashVars.getInstance().setData("domain", aDomain.join("/"));
			configureListeners();
            try {
                cURLLoader.load(new URLRequest(sURL));
            } catch (error:Error) {
                _fCallBackObject._function.apply(_fCallBackObject._object, [false]);
            }
		}
	    private function configureListeners():void {
			if(!cURLLoader){
				cURLLoader = new URLLoader();
			}
            cURLLoader.addEventListener(Event.COMPLETE, completeHandler);
            cURLLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            cURLLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
        }
		private function removeListeners():void{
            cURLLoader.removeEventListener(Event.COMPLETE, completeHandler);
            cURLLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            cURLLoader.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
		}
        private function completeHandler(event:Event):void {
			var xXML:XMLDocument = new XMLDocument(cURLLoader.data);
			_aThumbs = new Array();
			parseRequestVarsToData(xXML.firstChild.firstChild.childNodes);
			DataManager.getInstance().thumbs = _aThumbs;
			_fCallBackObject._function.apply(_fCallBackObject._object, [true]);
//			var cThumb:DataThumb = DataManager.getInstance().thumbs[0];
//			var cThumb1:DataThumb = DataManager.getInstance().thumbs[1];
//			trace("<<<<<<<<<<<<<<<<<<<<trace thumbs uffter parse");
//			trace("");
//			trace(cThumb.title, cThumb1.title);
//			trace(cThumb.description, cThumb1.description);
//			trace(cThumb.link, cThumb1.link);
//			trace(cThumb.id, cThumb1.id);
//			trace(cThumb.thumb, cThumb1.thumb);
//			trace(cThumb.image, cThumb1.image);
//			trace("");
//			trace("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
        }
		private function parseRequestVarsToData(aXMLNodesData:Array){
			for(var i in aXMLNodesData){
				if(aXMLNodesData[i].nodeName == "item"){
					_aThumbs.push(parseThumbData(aXMLNodesData[i].childNodes));
//					trace(aXMLNodesData[i].childNodes);
				}
			}
		}
		private function parseThumbData(aData:Array):DataThumb{
			var cData:DataThumb = new DataThumb();
			for(var i in aData){
				switch(aData[i].nodeName){
					case "title":
						cData.title = aData[i].firstChild;
						break;
					case "description":
						cData.description = aData[i].firstChild;
						break;
					case "link":
						cData.link = aData[i].firstChild;
						break;
					case "id":
						cData.id = aData[i].firstChild;
						break;
					case "thumbnail":
						cData.thumb = aData[i].firstChild;
						break;
					case "image":
						cData.image = aData[i].firstChild;
						break;						
				}
			}
			return cData;
		}
        private function securityErrorHandler(event:Event):void {
            removeListeners();
	        _fCallBackObject._function.apply(_fCallBackObject._object, [false]);
        }

        private function ioErrorHandler(event:IOErrorEvent):void {
            removeListeners();
            _fCallBackObject._function.apply(_fCallBackObject._object, [false]);
        }
		
		
	}
}